package com.example.usercenter.common.base;

import com.alibaba.fastjson.JSONObject;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zxn on 2017/10/23.
 */
public abstract class BaseRestController<T, PK extends Serializable> {
    protected abstract BaseService<T, PK> getService();

    @GetMapping(value="/{id}")
    @ResponseBody
    public ResponseData get(@PathVariable("id") PK id){
        T result = getService().get(id);
        if(result!=null)
            return ResponseData.ok("获取成功", result);
        return ResponseData.fail("获取失败");
    }

    @PostMapping(value="")
    @ResponseBody
    public ResponseData save(@RequestBody T entity){
        try {
            if(getService().insert(entity))
                return ResponseData.ok("保存成功");
        }
        catch (DuplicateKeyException ve){
            ve.printStackTrace();
            String message = ve.getCause().getMessage();
            return ResponseData.fail("保存失败："+message);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.fail("保存失败");
    }

    @PutMapping(value="")
    @ResponseBody
    public ResponseData update(@RequestBody T entity){
        try{
            if(getService().update(entity))
                return ResponseData.ok("修改成功");
        }
        catch (DuplicateKeyException ve){
            ve.printStackTrace();
            String message = ve.getCause().getMessage();
            return ResponseData.fail("保存失败："+message);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.fail("修改失败");
    }

    @DeleteMapping(value="/{id}")
    @ResponseBody
    public ResponseData delete(@PathVariable("id") PK id){
        try{
            if(getService().delete(id))
                return ResponseData.ok("删除成功");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.fail("删除失败");
    }

    @PostMapping(value="/page")
    @ResponseBody
    public ResponseData page(@RequestBody JSONObject params){
        try{
            return ResponseData.ok("分页查询成功", getService().findByPage(params));
        }
        catch (Exception e){
            return ResponseData.fail("分页查询异常");
        }
    }

    @PostMapping(value="/query")
    @ResponseBody
    public ResponseData query(@RequestBody JSONObject params){
        try{
            return ResponseData.ok("查询列表成功", getService().query(params));
        }
        catch (Exception e){
            return ResponseData.fail("查询列表异常");
        }
    }

    @PostMapping(value="/exists")
    @ResponseBody
    public ResponseData exists(@RequestBody JSONObject params){
        int result = getService().exists(params);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("exists", result>0);
        resultMap.put("result", result);
        return ResponseData.ok("查询成功", resultMap);
    }
}
